package com.hellobike.haxing.modules.wms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.util.oConvertUtils;
import com.hellobike.haxing.modules.wms.entity.Partition;
import com.hellobike.haxing.modules.wms.service.IPartitionService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.common.annotation.AutoLog;

/**
 * @Description: t_wms_partition
 * @Author: haxing-boot
 * @Date: 2020-02-08
 * @Version: V1.0
 */
@RestController
@RequestMapping("/wms/partition")
@Slf4j
public class PartitionController extends WmsBaseController<Partition, IPartitionService> {
    @Autowired
    private IPartitionService partitionService;

    /**
     * 分页列表查询
     *
     * @param partition
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "t_wms_partition-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(Partition partition,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        partition.setWarehouseId(getWarehouseId());
        QueryWrapper<Partition> queryWrapper = QueryGenerator.initQueryWrapper(partition, req.getParameterMap());
        Page<Partition> page = new Page<Partition>(pageNo, pageSize);
        IPage<Partition> pageList = partitionService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param partition
     * @return
     */
    @AutoLog(value = "t_wms_partition-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Partition partition) {
        Long warehouseId = getWarehouseId();
        Partition param = new Partition();
        param.setPartitionCode(partition.getPartitionCode());
        param.setWarehouseId(warehouseId);
        if (partitionService.getBaseMapper().selectOne(new QueryWrapper<>(param)) != null) {
            return Result.error("库区编码已被占用");
        }
        Partition param2 = new Partition();
        param2.setPartitionName(partition.getPartitionName());
        param2.setWarehouseId(warehouseId);
        if (partitionService.getBaseMapper().selectOne(new QueryWrapper<>(param2)) != null) {
            return Result.error("库区名称已被占用");
        }
        partition.setWarehouseId(warehouseId);
        partitionService.save(partition);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param partition
     * @return
     */
    @AutoLog(value = "t_wms_partition-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Partition partition) {
        Partition partition1 = partitionService.getById(partition.getId());
        boolean codeChange = false;
        if (!partition1.getPartitionCode().equals(partition.getPartitionCode())) {
            Partition param = new Partition();
		    param.setPartitionCode(partition.getPartitionCode());
            param.setWarehouseId(getWarehouseId());
            if (partitionService.getBaseMapper().selectOne(new QueryWrapper<>(param)) != null) {
                return Result.error("库区编码已被占用");
            }
            codeChange = true;
        }
        if (!partition1.getPartitionName().equals(partition.getPartitionName())) {
            Partition param = new Partition();
            param.setPartitionName(partition.getPartitionName());
            param.setWarehouseId(getWarehouseId());
            if (partitionService.getBaseMapper().selectOne(new QueryWrapper<>(param)) != null) {
                return Result.error("库区名称已被占用");
            }
        }
        partitionService.updateById(partition);
        if(codeChange){
            partitionService.updateRefLoc(partition.getId(),partition.getPartitionCode());
        }
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "t_wms_partition-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        partitionService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "t_wms_partition-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.partitionService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        Partition partition = partitionService.getById(id);
        if (partition == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(partition);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param partition
     */
    @AutoLog(value = "t_wms_partition-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Partition partition) {
        return super.exportXls(request, partition, Partition.class, "t_wms_partition");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "t_wms_partition-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Partition.class);
    }

}
